package cn.com.connext.scrm.web.rest;

import cn.com.connext.scrm.domain.master.Product;
import cn.com.connext.scrm.service.PrizeGoodService;
import cn.com.connext.scrm.service.WxAPIService;
import cn.com.connext.scrm.service.dto.JsonResult;
import cn.com.connext.scrm.service.mapper.ProductService;
import cn.com.connext.scrm.service.mapper.TmpMsgRecordService;
import cn.com.connext.scrm.web.rest.errors.MyErrorCode;
import cn.com.connext.scrm.web.rest.req.*;
import cn.com.connext.scrm.web.rest.resp.ProductResp;
import cn.com.connext.scrm.web.rest.vm.TempSubScribeVm;
import com.alibaba.fastjson.JSON;
import com.connext.common.exception.ServiceException;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

;import javax.crypto.MacSpi;
import java.util.HashMap;
import java.util.Map;

/**
 * 积分商城 - 实物商品管理
 */
@RestController
@RequestMapping("/api/shopping/product")
public class ProductResource {

    private final Logger log = LoggerFactory.getLogger(ProductResource.class);

    @Autowired
    private ProductService productService;
    @Autowired
    private PrizeGoodService prizeGoodService;
    @Autowired
    private TmpMsgRecordService tmpMsgRecordService;
    @Autowired
    private WxAPIService wxAPIService;

    /**
     * 创建产品列表
     */
    @PostMapping("/create")
    @ApiOperation(value = "创建产品管理", tags = {"产品列表"}, notes = "创建产品管理")
    public JsonResult save(@RequestBody ProductReq req) {
        log.info("创建产品管理，入参: {}", JSON.toJSONString(req));
        if (req == null) {
            throw new ServiceException(MyErrorCode.ERROR_INPUT);
        }
        // return productService.save(req);
        return productService.save(req);
    }

    /**
     * 查询产品列表
     */
    @PostMapping("/query")
    @ApiOperation(value = "查询产品管理--小程序", tags = {"产品管理列表"}, notes = "查询产品管理--小程序")
    public ResponseEntity<Page<ProductResp>> queryLists(@RequestBody ProduceQueryReq req) {
        log.info("查询产品管理--小程序,请求参数req : {}", JSON.toJSONString(req));
        Page ProductResps = productService.queryLists(req);
        return ResponseEntity.ok(ProductResps);
    }

    /**
     * 查询产品列表
     */
    @PostMapping("/pages")
    @ApiOperation(value = "查询产品管理--后台管理", tags = {"产品管理列表"}, notes = "查询产品管理--后台管理")
    public ResponseEntity<Page<ProductResp>> pages(@RequestBody ProduceQueryReq req) {
        log.info("查询产品管理--后台管理,请求参数req : {}", JSON.toJSONString(req));
        Page ProductResps = productService.pages(req);
        return ResponseEntity.ok(ProductResps);
    }

    /**
     * 产品管理删除
     */
    @PostMapping("/delete")
    @ApiOperation(value = "根据主键删除产品", tags = {"产品列表"}, notes = "根据主键删除产品")
    public JsonResult delete(@RequestBody DeleteReq req) {
        log.info("根据主键删除产品,请求参数req: {}", JSON.toJSONString(req));
        if (StringUtils.isEmpty(req.getId())) {
            throw new ServiceException(MyErrorCode.ERROR_INPUT);
        }
        JsonResult result = new JsonResult();
        try {
            return productService.delete(req.getId());
        } catch (Exception e) {
            log.error("删除失败:{}", e);
            result.setMessage("删除失败");
            result.setState(1);
            return result;
        }
    }

    /**
     * 是否上架
     */
    @PostMapping("/isAlive")
    @ApiOperation(value = "是否上架产品", tags = {"产品列表"}, notes = "是否上架产品")
    public JsonResult isAlive(@RequestBody DeleteReq req) {
        log.info("是否上架产品,请求参数req: {}", JSON.toJSONString(req));
        if (StringUtils.isEmpty(req.getId())) {
            throw new ServiceException(MyErrorCode.ERROR_INPUT);
        }
        return productService.isAlive(req.getId());
    }

    /**
     * 添加库存
     */
    @PostMapping("/stock")
    @ApiOperation(value = "添加库存", tags = {"产品列表"}, notes = "添加库存")
    public JsonResult stock(@RequestBody StockReq req) {
        log.info("添加库存,请求参数req: {}", JSON.toJSONString(req));
        if (StringUtils.isEmpty(req.getId()) || null == req.getStock() || null == req.getStockType()) {
            throw new ServiceException(MyErrorCode.ERROR_INPUT);
        }
        return productService.stock(req);
    }

    /**
     * 添加库存
     */
    @PostMapping("/detail")
    @ApiOperation(value = "根据主键删除产品", tags = {"产品列表"}, notes = "根据主键删除产品")
    public JsonResult detail(@RequestBody DeleteReq req) {
        log.info("根据主键删除产品,请求参数req: {}", JSON.toJSONString(req));
        if (StringUtils.isEmpty(req.getId())) {
            throw new ServiceException(MyErrorCode.ERROR_INPUT);
        }
        return productService.detail(req.getId());
    }

    /**
     * 通过产品id查询产品库存变更记录
     */
    @PostMapping("/getStockLogs")
    @ApiOperation(value = "根据主键删除产品", tags = {"产品列表"}, notes = "根据主键删除产品")
    public JsonResult getStockLogs(@RequestBody StockLogsReq req) {
        log.info("通过产品id查询产品库存变更记录,请求参数req: {}", JSON.toJSONString(req));
        if (StringUtils.isEmpty(req.getId())) {
            throw new ServiceException(MyErrorCode.ERROR_INPUT);
        }
        return productService.getStockLogs(req);
    }

    /**
     * 查询尊尼获加产品列表
     */
    @PostMapping("/findByPointManagerId")
    @ApiOperation(value = "查询产品管理--小程序", tags = {"产品管理列表"}, notes = "查询产品管理--小程序")
    public JsonResult findByPointManagerId(@RequestBody ProduceReq req) {
        JsonResult result =new JsonResult();
        log.info("查询产品管理--小程序,请求参数req : {}", JSON.toJSONString(req));
        Page list = productService.findByPointManagerId(req);
        result.setData(list);
        result.setState(0);
        result.setMessage("数据如上");
        return result;
    }


    /**
     * 抽中奖品发送订阅消息
     * @param vm
     * @return
     */
    @PostMapping("/getSubscribe")
    @ApiOperation(value = "抽中奖品发送订阅消息", notes = "抽中奖品发送订阅消息")
    public  JsonResult getSubscribe(@RequestBody TempSubScribeVm vm){
        return tmpMsgRecordService.sendTemplateMsg(vm);
    }

    /**
     * 定时任务发送订阅消息
     * @param
     * @return
     */
    //@Scheduled(cron = "0 0 */1 * * ?") //每小时执行一次
    //每个月20号,27号 10:00
    @Scheduled(cron = "0 0 10 20,27 * ?")
    public JsonResult sendExpireTemplateMsg() {
        JsonResult result = tmpMsgRecordService.sendExpireTimeMsg();
        return result;
    }


    /**
     * 定时任务发送订阅消息
     * @param
     * @return
     */

    //@Scheduled(cron = "0 0 */1 * * ?") //每小时执行一次
    //每个月20号,27号 10:00
    @Scheduled(cron = "0 0 12 * * ?") //每天中午12点执行；
    @GetMapping("/sendExpireTemplateMsgNew")
    public JsonResult sendExpireTemplateMsgNew() {
        log.info("定时发送过期提醒任务 开始执行----------------");
        JsonResult result = tmpMsgRecordService.sendNewExpireTimeMsg();
        log.info("定时发送过期提醒任务 执行完成----------------");
        return result;
    }


    //获取urlScheme
    @GetMapping("/getScheme")
    @ApiOperation(value = "抽中奖品发送订阅消息", notes = "抽中奖品发送订阅消息")
    public JsonResult getScheme() {
        JsonResult result = tmpMsgRecordService.getScheme();
        return result;
    }






}
